using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.Reflection;
using NHibernate;
using NHibernate.Cfg;


namespace IShop.BackEnd
{
    public class PersistenceManager : IDisposable
    {
        private ISessionFactory m_SessionFactory = null;
//        private ISession m_Session = null;
    
        public PersistenceManager()
        {
            this.ConfigureNHibernate();
        }

        public void  Dispose()
        {
 	        m_SessionFactory.Dispose();
        }

        public void Close()
		{
			m_SessionFactory.Close();
		}

        public void Save<T>(T item)
        {
            using (ISession session = m_SessionFactory.OpenSession())
            {
                using (session.BeginTransaction())
                {
                    session.SaveOrUpdate(item);
                    session.Transaction.Commit();
                }
            }
        }


        private void ConfigureNHibernate()
        {
            Configuration cfg = new Configuration();
            cfg.Configure();

            Assembly thisAssembly = typeof(ShopUser).Assembly;
            cfg.AddAssembly(thisAssembly);

            m_SessionFactory = cfg.BuildSessionFactory();
        }

    }
}
